package xin.core.jwt;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.security.SecureRandom;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * <pre>
 * My BCryptPasswordEncoder 现版本完全copy原BCryptPasswordEncoder的实现 方便以后调试与扩展
 * 复写encode
 * </pre>
 *
 * @author lixin
 * @version 1.0
 * @since 2017/11/23 17:12
 */
@Slf4j
public class MyBCryptPasswordEncoder implements PasswordEncoder {
    private Pattern BCRYPT_PATTERN;
    private final Log logger;
    private final int strength;
    private final SecureRandom random;

    public MyBCryptPasswordEncoder() {
        this(-1);
    }

    public MyBCryptPasswordEncoder(int strength) {
        this(strength, null);
    }

    public MyBCryptPasswordEncoder(int strength, SecureRandom random) {
        this.BCRYPT_PATTERN = Pattern.compile("\\A\\$2a?\\$\\d\\d\\$[./0-9A-Za-z]{53}");
        this.logger = LogFactory.getLog(this.getClass());
        if (strength == -1 || strength >= 4 && strength <= 31) {
            this.strength = strength;
            this.random = random;
        } else {
            throw new IllegalArgumentException("Bad strength");
        }
    }

    public String encode(CharSequence rawPassword) {
        String salt;
        if (this.strength > 0) {
            if (this.random != null) {
                salt = BCrypt.gensalt(this.strength, this.random);
            } else {
                salt = BCrypt.gensalt(this.strength);
            }
        } else {
            salt = BCrypt.gensalt();
        }
        log.info("salt:{}",salt);

        return BCrypt.hashpw(rawPassword.toString(), salt);
    }

    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        if (encodedPassword != null && encodedPassword.length() != 0) {
            if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) {
                this.logger.warn("Encoded password does not look like BCrypt");
                return false;
            } else {
                final boolean result = BCrypt.checkpw(rawPassword.toString(), encodedPassword);
                return result;
            }
        } else {
            this.logger.warn("Empty encoded password");
            return false;
        }
    }

    public static void main(String [] args){
        MyBCryptPasswordEncoder encoder = new MyBCryptPasswordEncoder();
//        lms.info("passwd : {}","123456");
        for(int i=0;i<5;i++){
            log.info("------------------------------------------------------------");
            log.info("io : {},", UUID.randomUUID().toString().replace("-", ""));
            String hash = "$2a$10$IE4iPvsL41.q6ZlGgfP6p.5PZHT5IdQE2irtSFIUsneM9Uudhzop2";
            log.info("hash : {}",encoder.encode("123456"));
        }
//        lms.info("match : {}",encoder.matches("123456",hash));

    }
}